
# 写一个单例,SingleInstance类


class Singlelnstance():

    instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.instance:
            cls.instance = object.__new__(cls)
        return cls.instance

p1 = Singlelnstance()
P2 = Singlelnstance()
print(p1 is P2)

#  第二种
#  第一个name  之后 不在创建

class Single():

    flag = False

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls,'instance'):
            cls.instance = object.__new__(cls)
        return cls.instance

    def __init__(self,name):
        if not Single.flag:
            self.name = name
            Single.flag = True


s1 = Single('QQ')
s2 = Single('WANG')
print(s1.name)
print(s2.name)








